package com.xwj.es.test;

import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.Arrays;

/**
 * @author: ciyuan
 * @date: 2021/5/4 0:33
 * description:
 */
public class EsDocBatchInsert {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );
        BulkRequest request = new BulkRequest();
        request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "joker1", "age", "30", "gender", "男"));
        request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "joker2", "age", "30", "gender", "男"));
        request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "joker3", "age", "30", "gender", "男"));
        request.add(new IndexRequest().index("user").id("1004").source(XContentType.JSON, "name", "joker4", "age", "30", "gender", "女"));
        request.add(new IndexRequest().index("user").id("1005").source(XContentType.JSON, "name", "joker5", "age", "30", "gender", "男"));
        request.add(new IndexRequest().index("user").id("1006").source(XContentType.JSON, "name", "joker6", "age", "30", "gender", "男"));
        request.add(new IndexRequest().index("user").id("1007").source(XContentType.JSON, "name", "joker7", "age", "30", "gender", "男"));
        BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
        System.out.println(response.getTook());
        System.out.println(Arrays.toString(response.getItems()));
        esClient.close();
    }
}
